//
//  MethodVC.swift
//  swiftDemo
//
//  Created by yaqiang bi on 2017/3/17.
//  Copyright © 2017年 毕小强. All rights reserved.
//
//  方法

import UIKit

class MethodVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
       ///实例方法
        class Counter{
            var count = 0
            func increment()  {
                count += 1
            }
            
            func increment(by amount:Int)  {
                count += amount
            }
            
            func reset (){
                count = 0
            }
        }
        let counter = Counter()
        counter.increment()
        counter.increment(by: 10)
        print(counter.count)
        counter.reset()
        print(counter.count)
        counter.count = 100
        print(counter.count)
        
        ///self属性
        struct Point {
            var x = 0.0,y = 0.0
            func isToTheRightOf(x:Double) -> Bool {
                return self.x > x
            }
            
            ///在实例方法中修改值类型
            mutating func moveByX(deltaX: Double, y deltaY: Double) {
                x += deltaX
                y += deltaY
            }
            
            mutating func moveBy(x deltaX:Double,y deltaY:Double){
                self = Point(x: x + deltaX, y: y + deltaY)
            }
        }
        var somePoint = Point(x:4.0,y:5.0)
        if  somePoint.isToTheRightOf(x: 1.0){
            print("This point is to the right")
        }
        somePoint.moveByX(deltaX: 20.0, y: 4.0)
        print("X:\(somePoint.x)  Y:\(somePoint.y)")
        somePoint.moveBy(x: 4.0, y: 8.0)
        print("X:\(somePoint.x)  Y:\(somePoint.y)")
        
        enum TriStateSwitch:String{
            case off,low,high
            mutating func next(){
                switch self {
                case .off:
                   self = .low
                case .low:
                    self = .high
                case .high:
                    self = .off
                }
            }
        }
        var ovenLight = TriStateSwitch.low
        ovenLight.next()
        print(ovenLight.rawValue)
        ovenLight.next()
        print(ovenLight.rawValue)
        
        ///类型方法
        class SomeClass{
            static func someTypeMethod(){
                print("你调用了一个类方法")
            }
        }
        SomeClass.someTypeMethod()
        
        struct LevelTracker{
            static var highestUnlockedLevel = 1
            var currentLevel = 1
            
            static func unlock(_ level:Int){
                if level > highestUnlockedLevel {
                    highestUnlockedLevel = level
                }
            }
            
            static func isUnlocked(_ level:Int)->Bool{
                return level <= highestUnlockedLevel
            }
            
            @discardableResult
            mutating func advance(to level: Int) -> Bool {
                if LevelTracker.isUnlocked(level) {
                    currentLevel = level
                    return true
                } else {
                    return false
                }
            }
        }
        
        LevelTracker.unlock(4)
        var xiaoming = LevelTracker()
        xiaoming.advance(to: 2)
        
        class Player{
            var tracker = LevelTracker()
            let playerName : String
            func completedLevel(level:Int) {
                LevelTracker.unlock(level+1)
                tracker.advance(to: level+1)
            }
            init(name:String) {
                playerName = name
            }
            
        }
        
        var player = Player(name:"Tom")
        player.completedLevel(level: 2)
        print("highest unlocked level is \(LevelTracker.highestUnlockedLevel)")
        
        player = Player(name:"Jack")
        if player.tracker.advance(to: 6) {
            print("player is now on level 6")
        }else {
            print("level 6 has not yet been unlocked")
        }
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
